<?php

namespace front\modules\app\controllers;

use Yii;
use yii\data\ActiveDataProvider;
use front\controllers\WebBaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backfeed\services\CommentService;
use yii\helpers\ArrayHelper;
use goods\services\ProductService;
use users\services\UserService;

/**
 * CommentController implements the CRUD actions for CommentModel model.
 */
class CommentController extends WebBaseController
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all CommentModel models.
     * @return mixed
     * 商品评论列表
     * www.wdt.com/comment/index?page=2&per-page=3&sort=+id
     */
    public function actionIndex()
    {
        $data = yii::$app->request->get();
        $data['user_id'] = $this->user_id;
        $data['getByWeb']=1;
        $info = CommentService::search($data);
        $info['result'] =ArrayHelper::toArray($info['result']);
        $arr = array_map(function($v) {
           $v['created_at'] = date('Y-m-d H:i:s',$v['created_at']);
           $v['product'] = ProductService::ProductInfo($v['product_id']);
           $v['product'] = ArrayHelper::toArray($v['product']);
           $v['product'] = $v['product']['result']['productInfo']['item_url_map'];
           $v['user_name'] = UserService::getUserInfo($v['user_id']);
           $v['user_name'] =ArrayHelper::toArray($v['user_name']['result']);;
           return $v;
        }, $info['result']);
        foreach($arr as $k=>$v){
            foreach($v['product'] as $v1){
                if($v['product_id']==$v1['product_id']){
                    $arr[$k]['product'] = $v1;
                }
            }
        }
        $arr['count'] = count($arr);
        WebBaseController::getAjaxData(1,'获取成功',$arr);
    }


    /**
     * 插入评论
     */

    public function actionAdd()
    {

       $data['product_id'] = intval(yii::$app->request->get('product_id'));
       $data['user_id'] = $this->user_id;
       $data['content'] =strip_tags(yii::$app->request->get('content'));
       $data['created_at']=time();
       if(!$data['product_id']){
           WebBaseController::getAjaxData(2,'商品不存在');
       }
        if(!$data['user_id']){
            WebBaseController::getAjaxData(3,'用户不存在');
        }
        if(!$data['content']){
            WebBaseController::getAjaxData(4,'内容不能为空');
        }
       $info = CommentService::add($data);
       if($info['code']==1){
           WebBaseController::getAjaxData(5,'添加失败');
       }
        WebBaseController::getAjaxData(1,'添加成功',$info['result']);
    }

    /**
     * Updates an existing CommentModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing CommentModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the CommentModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return CommentModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = CommentModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
